package chapter07

object Test18_ComplexWordCount {
  def main(args: Array[String]): Unit = {
    val tupleList:List[(String,Int)] = List(("hello", 1), ("hello world", 2),
      ("hello scala", 3), ("hello spark from scala", 1),
      ("hello flink from scala",2))
    /*
    * 假设已经有一些预处理的数据，你还是希望计算这些单词的个数，那么我们可以把
    * 他们还原到之前的样子
    *
    * 我们希望，将他们还原回原始的字符串的形式
    * */
    val strings: List[String] = tupleList.map(kv => {
      (kv._1 + "") * kv._2
    })
    // 然后是老样子
    val strings1: List[String] = strings.flatMap(_.split(" "))
    val stringToStrings: Map[String, List[String]] = strings1.groupBy(s => s)
    val stringToInt: Map[String, Int] = stringToStrings.map(kv => (kv._1, kv._2.length))
    val tuples: List[(String, Int)] = stringToInt.toList.sortWith(_._2 > _._2).take(3)

    // 你也可以连起来写
    val tuples1: List[(String, Int)] = tupleList.map(kv => (kv._1 + "") * kv._2)
      .flatMap(_.split(" "))
      .groupBy(s => s)
      .map(kv => (kv._1, kv._2.size))
      .toList
      .sortBy(_._2)(Ordering[Int].reverse)
      .take(3)

    // 另一种实现思路
    /*
    * 对于之前的原始数据  ，直接做转换
    * */
    val tuples2: List[(String, Int)] = tupleList.flatMap(tuples => {
      val strings2: Array[String] = tuples._1.split(" ")
      strings2.map(s => (s, tuples._2))
    })
    val stringToTuples: Map[String, List[(String, Int)]] = tuples.groupBy(s => s._1)

    // 只更新value
    val stringToInt1: Map[String, Int] = stringToTuples.mapValues(kv => kv.map(_._2).sum)
  }
}
